import { _decorator, Collider2D, Component, Node, Vec3,ITriggerEvent, Contact2DType, BoxCollider2D } from 'cc';
import { loading } from './loading';
import { store } from './store';
const { ccclass, property } = _decorator;

@ccclass('pipe')
export class pipe extends Component {
    // 自动向左走
    @property(Number)
    // 每一秒走过的距离
    public speed:number = 0
    // public birdCollider:Collider2D = null
    protected onLoad(): void {
        for(let child of this.node.children){
            // 注册触发器事件
            // child.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT,this.judgeType,this)  
        }
    }
    start() {
        
    }
    // 根据碰撞的类型来判断与修改得分以及游戏的状态
    judgeType(selfCollider:Collider2D,otherCollider:Collider2D){
        let tag = selfCollider.node.getComponent(Collider2D).tag
        console.log('当前触发器tag：' +  tag)
    }

    update(deltaTime: number) {
        // 游戏不是结束状态才移动
        if(loading.gameState){
            // 当前坐标
            const oldPos = this.node.getWorldPosition()
            // 设置新坐标 向前移动
            this.node.setWorldPosition(new Vec3(oldPos.x - store.speed*deltaTime , oldPos.y , 0 ))
        }
        // 判断每个节点是不是已经离开了场景 如果离开了 就销毁
        if(this.node.getWorldPosition().x < -52){
            this.node.destroy()
        }
    }
}


